import sqlite3

class user():
    def insert(self):
        conn = sqlite3.connect('taobao.db')
        cursor = conn.cursor()
        username = input('请输入用户名：')
        sql = 'select * from users where username = ?'
        cursor.execute(sql, (username,))
        if cursor.fetchone():
            print('用户已存在,是否更新数据')
            choice = input('1.更新数据 2.取消 请选择: ')
            if choice == '1':
                password = input('请输入密码：')
                sql = 'update users set password = ? where username = ?'
                cursor.execute(sql, (password, username))
                conn.commit()
                print('更新成功')
            else:
                print('取消更新')
        else:
            password = input('请输入密码：')
            sql = 'insert into users (username, password) values (?, ?)'
            cursor.execute(sql, (username, password))
            conn.commit()
            print('添加成功')

    def delete(self):
        conn = sqlite3.connect('taobao.db')
        cursor = conn.cursor()
        username = input('请输入用户名：')
        sql = 'select * from users where username = ?'
        cursor.execute(sql, (username,))
        if cursor.fetchone():
            sql = 'delete from users where username = ?'
            cursor.execute(sql, (username,))
            conn.commit()
            print('删除成功')
        else:
            print('查无此人')


if __name__ == '__main__':

    conn = sqlite3.connect('taobao.db')
    cursor = conn.cursor()
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username VARCHAR(20) NOT NULL UNIQUE,
        password VARCHAR(20) NOT NULL
    )
    ''')

    while True:
        print("=====欢迎来到淘宝=====")
        print("1. 增加用户")
        print("2. 删除用户")
        print("0. 退出")
        choice = input("请输入你的选择：")
        if choice == '1':
            user = user()
            user.insert()
        elif choice == '2':
            user = user()
            user.delete()
        elif choice == '0':
            break
        else:
            print("输入错误，请重新输入")

